package com.phs.main.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.web.po.PropertyInformation;
import com.phs.main.web.po.Repair;
import com.phs.main.web.po.SysUsers;
import com.phs.main.web.service.IPropertyInformationService;
import com.phs.main.web.service.IRepairService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 报修维修表 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/repair")
public class RepairController {
	@Lazy
	@Autowired
	IRepairService repairService;
	@Lazy
	@Autowired
	IPropertyInformationService propertyInformationService;

	@ApiOperation(value = "添加报修维修记录")
	@PostMapping("/addRepair")
	public AjaxResult addRepair(@RequestBody Map<String, String> map) {
		try {
			QueryWrapper<PropertyInformation> propInfoWrapper = new QueryWrapper<>();
			propInfoWrapper.eq("house_unit", map.get("houseUnit"))
					.eq("house_name", map.get("houseName"));
			PropertyInformation one = propertyInformationService.getOne(propInfoWrapper);
			Date t = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(sdf.format(t));
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

			LocalDateTime ldt = LocalDateTime.parse(sdf.format(t),df);
			System.out.println("String类型的时间转成LocalDateTime："+ldt);
			Repair repair = new Repair();
			repair.setRName(map.get("rName"));
			repair.setPhone(map.get("phone"));
			repair.setRDetail(map.get("rDetail"));
			repair.setRepairType(Integer.valueOf(map.get("repairType")));
			repair.setUserId(((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());
			repair.setRTime(ldt);
			repair.setHouseId(one.getHouseId());
			repairService.save(repair);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除报修维修记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "报修维修记录ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteRepairById/{id}")
	public AjaxResult deleteRepairById(@PathVariable long id) {
		try {
			if (repairService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新报修维修记录")
	@PutMapping("/updateRepair")
	public AjaxResult updateRepair(@RequestBody Repair repair) {
		try {
			if (repairService.updateById(repair)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有单元号")
	@GetMapping("/getUnit")
	public AjaxResult getUnit() {
		return AjaxResult.success(propertyInformationService.list());
	}

	@ApiOperation("查询所有房间号")
	@GetMapping("/getRooms")
	public AjaxResult getRooms() {
		return AjaxResult.success();
	}

	@ApiOperation("查询所有报修维修记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<Repair> page = new Page<>(current , size);
		return AjaxResult.success(repairService.page(page));
	}

	@ApiOperation("按照报修类型查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "repairType", value = "报修类型", dataType = "Long")
	})
	@GetMapping("/findByType/{repairType}")
	public AjaxResult findByType(@PathVariable Long repairType) {
		QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
		repairQueryWrapper.like("repair_type", repairType);

		return AjaxResult.success(repairService.list(repairQueryWrapper));
	}

	@ApiOperation("按照业主id和维修状态查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "mstatus", value = "维修状态", dataType = "Long")
	})
	@GetMapping("/findByIDStatus/{mstatus}")
	public AjaxResult findByUserID(@PathVariable Long mstatus) {
		QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
		repairQueryWrapper.like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId()).like("m_status",mstatus);
		return AjaxResult.success(repairService.list(repairQueryWrapper));
	}

	@ApiOperation("按照用户id和报修类型查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "repairType", value = "报修类型", dataType = "Long")
	})
	@GetMapping("/findByIDType/{repairType}")
	public AjaxResult findByIDType(@PathVariable Long repairType) {
		QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
		repairQueryWrapper.like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId()).like("repair_type", repairType);

		return AjaxResult.success(repairService.list(repairQueryWrapper));
	}

	@ApiOperation("按照维修情况查询")
	@ApiImplicitParams({
	})
	@GetMapping("/findByID")
	public AjaxResult findByID() {
		QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
		repairQueryWrapper.like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());

		return AjaxResult.success(repairService.list(repairQueryWrapper));
	}

	@ApiOperation("按照维修情况查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "mstatus", value = "维修状态", dataType = "Long")
	})
	@GetMapping("/findByStatus/{mstatus}")
	public AjaxResult findByStatus(@PathVariable Long mstatus) {
		QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
		repairQueryWrapper.like("m_status", mstatus);

		return AjaxResult.success(repairService.list(repairQueryWrapper));
	}


}
